import requests,json,pymongo
from lxml import etree
from pymongo.collection import Collection
class Dangdang():
    client=pymongo.MongoClient(host='127.0.0.1',port=27017)
    db=client['dangdang_db']
    def __init__(self):
        self.header={
            'Host':'bang.dangdang.com',
            'Connection':'keep-alive',
            'Upgrade-Insecure-Requests':'1',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55',
            'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Encoding':'gzip,deflate',
            'Accept-Language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'
        }
        self.dangdang=Collection(Dangdang.db,'praise_rank')
    def get_dangdang(self,page):
        url='http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1-%d'%page
        response=requests.get(url=url,headers=self.header)
        if response:
            html=etree.HTML(response.text)
            items=html.xpath('//ul[@class="bang_list clearfix bang_list_mode"]/li')
            return items
    def join_list(self,item):
        return "".join(item)
    def parse_item(self,items):
        result_list=[]
        for item in items:
            title=item.xpath('.//div[@class="name"]/a/text()')
            comment=item.xpath('.//div[@class="star"]/a/text()')
            recommend=item.xpath('.//div[@class="star"]/span[@class="tuijian"]/text()')
            author=item.xpath('.//div[@class="publisher_info"][1]/a[1]/@title')
            publication_time=item.xpath('.//div[@class="publisher_info"][2]/span/text()')
            press=item.xpath('.//div[@class="publisher_info"][2]/a/text()')
            score=item.xpath('.//div[@class="biaosheng"]//text()')
            price=item.xpath('.//div[@class="price"]/p[1]/span[1]/text()')
            discount=item.xpath('.//div[@class="price"]/p/span[@class="price_s"]/text()')
            e_book=item.xpath('.//div[@class="price"]/p[@class="price_e"]/span[@class="price_n"]/text()')
            result_list.append(
                {
                    'title':self.join_list(title),
                    'comment':self.join_list(comment),
                    'recommend':self.join_list(recommend),
                    'author':self.join_list(author),
                    'publication_time':self.join_list(publication_time),
                    'press':self.join_list(press),
                    'score':self.join_list(score),
                    'price':self.join_list(price),
                    'discount':self.join_list(discount),
                    'e_book':self.join_list(e_book)
                }
            )
        return result_list
    def insert_data(self,result_list):
        self.dangdang.delete_many({})
        self.dangdang.insert_many(result_list)
def main():
    d=Dangdang()
    for p in range(1,26):
        items=d.get_dangdang(page=p)
        result=d.parse_item(items=items)
        d.insert_data(result_list=result) 
if __name__ == '__main__':
    main()
